import errorEvent from '../../errorMessage'
import store from '@/store/index'
import httpService from '../../httpService';
export default {
    async getDetail(id) {
        var serviceAddress = store.state.mainOptions.service;
        try {
            var data = await httpService({
                type: 'get',
                url: 'http://' + serviceAddress + '/project/deteil',
                params: {
                    id: id,
                }
            });
            return data;
        } catch (error) {
            errorEvent(error);
            return false;
        }
    },
    async editDetail(id, p) {
        var serviceAddress = store.state.mainOptions.service;
        var token = store.state.mainOptions.token;
        try {
            var data = await httpService({
                type: 'put',
                url: 'http://' + serviceAddress + '/project',
                data: {
                    id: id,
                    title: p.title,
                    description: p.description,
                },
                headers: { token: token },
            });
            return data;
        } catch (error) {
            errorEvent(error);
            return false;
        }
    },
    // /project/status
    async getStatusGrounp(id) {
        var serviceAddress = store.state.mainOptions.service;
        try {
            var data = await httpService({
                type: 'get',
                url: 'http://' + serviceAddress + '/project/status',
                params: {
                    id: id,
                },
            });
            return data;
        } catch (error) {
            errorEvent(error);
            return false;
        };
    },
    async getProjectUser() {
        var projectId = store.state.mainOptions.project.projectId;
        var serviceAddress = store.state.mainOptions.service;
        try {
            var data = await httpService({
                type: 'get',
                url: 'http://' + serviceAddress + '/project/userList',
                params: {
                    projectId: projectId,
                }
            });
            return data;
        } catch (error) {
            errorEvent(error);
            return false;
        }
    },
    async upDataProjectUserPermission(data) {
        var projectId = store.state.mainOptions.project.projectId;
        var serviceAddress = store.state.mainOptions.service;
        var token = store.state.mainOptions.token;
        try {
            var _data = await httpService({
                type: 'put',
                url: 'http://' + serviceAddress + '/project/Permission',
                headers: { token: token },
                data: {
                    id: data.id,
                    permission: data.permission,
                },
                params: { projectId: projectId, }
            });
            return _data;
        } catch (error) {
            errorEvent(error);
            return false;
        }
    },
    async deleteProjectUserPermission(id) {
        var projectId = store.state.mainOptions.project.projectId;
        var serviceAddress = store.state.mainOptions.service;
        var token = store.state.mainOptions.token;
        try {
            var data = await httpService({
                type: 'delete',
                url: 'http://' + serviceAddress + '/project/User',
                headers: { token: token },
                data: { id: id },
                params: {
                    projectId: projectId,
                }
            });
            return data;
        } catch (error) {
            errorEvent(error);
            return false;
        }
    },
    async searchUser(username) {
        var serviceAddress = store.state.mainOptions.service;
        try {
            var data = await httpService({
                type: 'post',
                url: 'http://' + serviceAddress + '/project/searchUser',
                data: { username: username },
            });
            return data;
        } catch (error) {
            errorEvent(error);
            return false;
        }
    },
    async addUser(data) {
        var projectId = store.state.mainOptions.project.projectId;
        var serviceAddress = store.state.mainOptions.service;
        var token = store.state.mainOptions.token;
        try {
            var _data = await httpService({
                type: 'post',
                url: 'http://' + serviceAddress + '/project/addUser',
                data: { userId: data.userId, permission: data.permission },
                headers: { token: token },
                params: { projectId: projectId, }
            });
            return _data;
        } catch (error) {
            errorEvent(error);
            return false;
        };
    },
}